package com.xca.znb.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xca.znb.R;
import com.xca.znb.entity.Goods;

import java.util.List;

public class SearchAdapter extends BaseAdapter {
    private Context context ;
    private List<Goods> goodsList ;

    public SearchAdapter(Context context , List<Goods> goodsList){
        this.context = context ;
        this.goodsList = goodsList ;
    }


    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context) ;
            convertView = inflater.inflate(R.layout.search_list_item , parent , false);
            viewHolder = new ViewHolder() ;
            viewHolder.showFirstImg = convertView.findViewById(R.id.searchItem_goods_img_show);
            viewHolder.showName = convertView.findViewById(R.id.searchItem_show_goods_name) ;
            viewHolder.showId = convertView.findViewById(R.id.searchItem_goods_show_goodsId);
            viewHolder.showLocal = convertView.findViewById(R.id.searchItem_show_goods_local) ;
            viewHolder.showPrice = convertView.findViewById(R.id.searchItem_show_goods_price);
            viewHolder.showUnit = convertView.findViewById(R.id.searchItem_show_goods_unit);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Goods goods = goodsList.get(position) ;
        Bitmap bitmap = stringPathToBitmap(goods.getFirst_image());
        if (bitmap != null){
            viewHolder.showFirstImg.setImageBitmap(bitmap);
        }else {
            viewHolder.showFirstImg.setImageResource(R.drawable.add_photo);
        }
        viewHolder.showName.setText(goods.getName());
        int id = goods.getId();
        String goodsId = String.valueOf(id) ;
        viewHolder.showId.setText(goodsId);
        viewHolder.showLocal.setText(goods.getAddress());
        viewHolder.showPrice.setText(String.valueOf(goods.getPrice()));
        viewHolder.showUnit.setText(goods.getUnit());

        viewHolder.showId.setTag(position);

        return convertView;
    }

    static class ViewHolder{
        ImageView showFirstImg ;
        TextView showName ;
        TextView showId ;
        TextView showLocal ;
        TextView showPrice ;
        TextView showUnit ;
    }

    public Bitmap stringPathToBitmap(String imagePath) {
        try {
            // 使用BitmapFactory.decodeFile方法将图片路径转换为Bitmap对象
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

            if (bitmap != null) {
                return bitmap;
            } else {
                // 图片加载失败处理
                // Log.e("ImageLoadError", "Failed to load image from path: " + imagePath);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 异常处理
            return null;
        }
    }
}
